package com.cvo.scrumtoolkit.client.pannels.widgets;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.HistoryManager;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.entities.WorkItem;
import com.cvo.scrumtoolkit.client.model.ProjectStore;
import com.cvo.scrumtoolkit.client.pannels.PersistentService;
import com.cvo.scrumtoolkit.client.pannels.PersistentServiceAsync;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.NumberLabel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.TextBox;

public class BacklogInSprintWidget extends Composite {
	HorizontalPanel hp = new HorizontalPanel();
	HorizontalPanel achterlPanel = new HorizontalPanel();
	TextBox invulvakWorkitem = new TextBox();
	private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);
	
	
	
	public BacklogInSprintWidget(final BacklogItem b){
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		initWidget(hp);
		hp.setSize("400px", "40px");
		/*ProjectStore projectStore=ProjectStore.getProjectStore();	
		try {
			project = projectStore.getProjectById(Long.parseLong(Navigator.getNavigator().getProjectId()));
			
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		
		
		HorizontalPanel voorkantPanel = new HorizontalPanel();
		voorkantPanel.setStyleName("backlog");
		hp.add(voorkantPanel);
		hp.setCellVerticalAlignment(voorkantPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		voorkantPanel.setSize("400px", "40px");
		hp.setCellWidth(voorkantPanel, "400");
		
		Label lblBacklogitemnaam = new Label("backlogItemNaam");
		voorkantPanel.add(lblBacklogitemnaam);
		voorkantPanel.setCellVerticalAlignment(lblBacklogitemnaam, HasVerticalAlignment.ALIGN_MIDDLE);
		hp.setCellVerticalAlignment(lblBacklogitemnaam, HasVerticalAlignment.ALIGN_MIDDLE);
		lblBacklogitemnaam.setText(b.getBacklogItemName());
		lblBacklogitemnaam.setSize("150px", "20px");
		
		Label lblGeschattetijd = new Label("geschatteTijd");
		voorkantPanel.add(lblGeschattetijd);
		voorkantPanel.setCellVerticalAlignment(lblGeschattetijd, HasVerticalAlignment.ALIGN_MIDDLE);
		voorkantPanel.setCellHorizontalAlignment(lblGeschattetijd, HasHorizontalAlignment.ALIGN_CENTER);
		lblGeschattetijd.setText(b.getGeschatteTijd()+"");
		hp.setCellHorizontalAlignment(lblGeschattetijd, HasHorizontalAlignment.ALIGN_CENTER);
		hp.setCellVerticalAlignment(lblGeschattetijd, HasVerticalAlignment.ALIGN_MIDDLE);
		lblGeschattetijd.setSize("100px", "20px");
		
		Button btnWorkitemtoevoegen = new Button("workitemToevoegen");
		voorkantPanel.add(btnWorkitemtoevoegen);
		voorkantPanel.setCellHorizontalAlignment(btnWorkitemtoevoegen, HasHorizontalAlignment.ALIGN_RIGHT);
		btnWorkitemtoevoegen.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				achterlPanel.setVisible(true);
			//	PopupWorkitemAanmaken p = new PopupWorkitemAanmaken(b);
			//	RootPanel.get().add(p);
			//	RootPanel.get().setWidgetPosition(p,1000, 400);
			//		p.setStyleName("main");
				
				
			}
		});
		btnWorkitemtoevoegen.setText("Add");
		hp.setCellVerticalAlignment(btnWorkitemtoevoegen, HasVerticalAlignment.ALIGN_MIDDLE);
		hp.setCellHorizontalAlignment(btnWorkitemtoevoegen, HasHorizontalAlignment.ALIGN_RIGHT);
		btnWorkitemtoevoegen.setSize("50px", "39px");
		
		
		hp.add(achterlPanel);
		
		
		achterlPanel.add(invulvakWorkitem);
		invulvakWorkitem.setSize("200px", "30px");
		
		Button btnOk = new Button("ok");
		btnOk.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String s= invulvakWorkitem.getText();
				invulvakWorkitem.setText("");
				persistentService.persistWorkitem(b.getBacklogItemName(), s, "Yves Bosmans", new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("Workitem is NIET aangemaakt");
						
					}

					@Override
					public void onSuccess(Void result) {
						System.out.println("Workitem is aangemaakt");
						wachteenbeetje();
					}
				});
				
				
				
				
				
				achterlPanel.setVisible(false);	
			}
		});
		achterlPanel.add(btnOk);
		btnOk.setSize("40px", "43px");
		achterlPanel.setVisible(false);
	}
	public void wachteenbeetje(){
		Timer t= new Timer(){

			@Override
			public void run() {
				try{
					String	id =Navigator.getNavigator().getProjectId();
					String	sprint= Navigator.getNavigator().getSprintId();
						String string = "sprintbacklog/"+id+"/"+sprint;
						HistoryManager.gotoUrl(string);

					}catch (Exception ex) {
					// TODO: handle exception
					}
				
			}
			
		};
		t.schedule(7000);
	}
}
